package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.TrayMapper;
import com.canaan.business.domain.bill.Tray;
import com.canaan.business.service.bill.ITrayService;
import com.canaan.common.annotation.TableName;
/**
 * 托盘Service业务层处理
 *
 * @author canaan
 * @date 2025-03-30
 */
@Service
public class TrayServiceImpl implements ITrayService {
    @Resource
    private TrayMapper trayMapper;

    /**
     * 查询托盘
     *
     * @param id 托盘主键
     * @return 托盘
     */
    @Override
    public Tray selectTrayById(Long id) {
        return trayMapper.selectTrayById(id);
    }

    /**
     * 查询托盘
     *
     * @param trayNo 托盘号
     * @return 托盘
     */
    @Override
    public Tray selectTrayByTrayNo(String trayNo) {
        return trayMapper.selectTrayByTrayNo(trayNo);
    }

    /**
     * 查询托盘列表
     *
     * @param tray 托盘
     * @return 托盘
     */
    @Override
    public List<Tray> selectTrayList(Tray tray) {
        return trayMapper.selectTrayList(tray);
    }

    /**
     * 新增托盘
     *
     * @param tray 托盘
     * @return 结果
     */
    @Override
    public int insertTray(Tray tray) {
        tray.setCreateBy(SecurityUtils.getUsername());
        tray.setCreateTime(DateUtils.getNowDate());
        return trayMapper.insertTray(tray);
    }

    /**
     * 批量新增托盘
     *
     * @param trayList 托盘列表
     * @return 结果
     */
    @Override
    public int batchInsertTray(List<Tray> trayList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Tray tray : trayList){
            tray.setCreateBy(username);
            tray.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<trayList.size();) {
            int endIndex = i+500;
            if (endIndex > trayList.size()) {
                endIndex = trayList.size();
            }
            insertNum = insertNum + trayMapper.batchInsertTray(trayList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改托盘
     *
     * @param tray 托盘
     * @return 结果
     */
    @Override
    public int updateTray(Tray tray) {
        tray.setUpdateBy(SecurityUtils.getUsername());
        tray.setUpdateTime(DateUtils.getNowDate());
        return trayMapper.updateTray(tray);
    }

    /**
     * 批量修改托盘
     *
     * @param trayList 托盘列表
     * @return 结果
     */
    @Override
    public int batchUpdateTray(List<Tray> trayList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Tray tray : trayList){
            tray.setUpdateBy(username);
            tray.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<trayList.size();) {
            int endIndex = i+500;
            if (endIndex > trayList.size()) {
                endIndex = trayList.size();
            }
            updateNum = updateNum + trayMapper.batchUpdateTray(trayList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除托盘
     *
     * @param ids 需要删除的托盘主键集合
     * @return 结果
     */
    @Override
    public int deleteTrayByIds(Long[] ids) {
        return trayMapper.deleteTrayByIds(ids);
    }

    /**
     * 删除托盘信息
     *
     * @param id 托盘主键
     * @return 结果
     */
    @Override
    public int deleteTrayById(Long id) {
        return trayMapper.deleteTrayById(id);
    }
}
